﻿

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <link href="~/Content/layui/css/layui.css" rel="stylesheet" />
    <script src="~/Content/layui/layui.js"></script>
    <script src="~/Content/myjs/jquery-1.10.2.min.js"></script>
</head>
<body>
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
        <legend>为 @ViewBag.roleId 配置权限</legend>
    </fieldset>
    <div style="margin-left:20%">
        <div class="layui-btn-container">
            <button type="button" class="layui-btn layui-btn-sm" lay-demo="getChecked">点击配置权限</button>
            <button type="button" class="layui-btn layui-btn-sm" lay-demo="reload">重置权限</button>
        </div>

        <div id="test12" class="demo-tree-more"></div>
    </div>

    <!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
    <script>
        layui.use(['tree', 'util'], function () {
            var tree = layui.tree
                , layer = layui.layer
                , util = layui.util

            //基本演示
            tree.render({
                elem: '#test12'
                , data: getData()
                , showCheckbox: true  //是否显示复选框
                , id: 'demoId1'
                //, isJump: true //是否允许点击节点时弹出新窗口跳转
                //, click: function (obj) {
                //    var data = obj.data;  //获取当前点击的节点数据
                //    layer.msg('状态：' + obj.state + '<br>节点数据：' + JSON.stringify(data));
                //}
            });

            //按钮事件
            util.event('lay-demo', {
                getChecked: function (othis) {
                    debugger;



                    

                    var checkedData = tree.getChecked('demoId1'); //获取选中节点的数据
                    //转成字符串
                    var str = JSON.stringify(checkedData);
                    //即将构造字符串，获取id（其实就是powerId）
                    var strId = "";
                    //字符串分割
                    var arr = str.split('"id"');
                    //循环构造
                    if (arr.length > 1) {

                        layer.confirm('确认要配置权限吗?', function(index){
                             for (var i = 2; i < arr.length; i++) {
                            strId += arr[i].split(',')[0].split(':')[1].split('"')[1].split('"')[0]+",";
                        }
                        //调用方法，提交请求
                        SetPower(strId);

                        layer.close(index);
                        });

                      

                    } else {
                        layer.alert("请选择需要配置的权限");
                    }
                 
                }
                , reload: function () {
                    //重载实例
                    tree.reload('demoId1', {

                    });

                }
            });

            //给角色配置权限
            function SetPower(strId) {

                $.ajax({
                    url: "/Role/SetPower",    //后台数据请求地址
                    data: {
                        roleId:"@ViewBag.roleId",
                        strId: strId,
                    },
                    type: "post",
                    success: function (data) {
                        if (data=="ok") {
                            layer.alert("配置成功");
                            //跳转页面
                             window.location.href = "/Role/PowerIndex?roleId="+@ViewBag.roleId+"";
                        } else {
                             layer.alert("配置失败");
                        }
                    }
                });

            }

            //获取目录树数据信息
            function getData() {

                var data = [];

                $.ajax({
                    url: "/Role/GetPowerList",    //后台数据请求地址
                    type: "post",
                     data: {
                        roleId:"@ViewBag.roleId",
                    },
                    async: false,//重点
                    success: function (resut) {
                        debugger;
                        data = JSON.parse(resut);

                    }
                });
                return data;
            }

        });
    </script>

</body>


</html>
